#[cfg(test)]
mod tests {
    use crate::{DefaultCircuitBreaker, CircuitBreakerConfig};
    use rustcloud_core::{CircuitBreaker, CircuitBreakerState, Response, ServiceError};
    use std::time::Duration;
    use tokio::time::sleep;

    #[tokio::test]
    async fn test_circuit_breaker_creation() {
        let config = CircuitBreakerConfig::default();
        let cb = DefaultCircuitBreaker::new(config);
        
        let state = cb.state("test-service").await;
        assert_eq!(state, CircuitBreakerState::Closed);
    }

    #[tokio::test]
    async fn test_circuit_breaker_successful_call() {
        let config = CircuitBreakerConfig::default();
        let cb = DefaultCircuitBreaker::new(config);
        
        let result = cb.call(
            "test-service",
            Box::new(|| {
                Box::pin(async {
                    Ok(Response::new(200))
                })
            })
        ).await;
        
        assert!(result.is_ok());
        let state = cb.state("test-service").await;
        assert_eq!(state, CircuitBreakerState::Closed);
    }

    #[tokio::test]
    async fn test_circuit_breaker_failure_threshold() {
        let mut config = CircuitBreakerConfig::default();
        config.failure_threshold = 3;
        config.recovery_timeout = Duration::from_millis(100); // 100ms for faster testing
        let cb = DefaultCircuitBreaker::new(config);
        
        // 模拟连续失败
        for _ in 0..3 {
            let result = cb.call(
                "test-service",
                Box::new(|| {
                    Box::pin(async {
                        Err(ServiceError::NetworkError("Connection failed".to_string()))
                    })
                })
            ).await;
            assert!(result.is_err());
        }
        
        // 应该进入打开状态
        let state = cb.state("test-service").await;
        assert_eq!(state, CircuitBreakerState::Open);
        
        // 下一次调用应该快速失败
        let result = cb.call(
            "test-service",
            Box::new(|| {
                Box::pin(async {
                    Ok(Response::new(200))
                })
            })
        ).await;
        
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), ServiceError::CircuitBreakerOpen(_)));
    }

    #[tokio::test]
    async fn test_circuit_breaker_half_open_recovery() {
        let mut config = CircuitBreakerConfig::default();
        config.failure_threshold = 2;
        config.recovery_timeout = Duration::from_millis(50); // 50ms for faster testing
        config.half_open_success_threshold = 2;
        let cb = DefaultCircuitBreaker::new(config);
        
        // 模拟失败使熔断器打开
        for _ in 0..2 {
            let _ = cb.call(
                "test-service",
                Box::new(|| {
                    Box::pin(async {
                        Err(ServiceError::NetworkError("Connection failed".to_string()))
                    })
                })
            ).await;
        }
        
        assert_eq!(cb.state("test-service").await, CircuitBreakerState::Open);
        
        // 等待恢复时间
        sleep(Duration::from_millis(60)).await;
        
        // 第一次调用应该进入半开状态
        let result = cb.call(
            "test-service",
            Box::new(|| {
                Box::pin(async {
                    Ok(Response::new(200))
                })
            })
        ).await;
        
        assert!(result.is_ok());
        
        // 再次成功调用应该关闭熔断器
        let result = cb.call(
            "test-service",
            Box::new(|| {
                Box::pin(async {
                    Ok(Response::new(200))
                })
            })
        ).await;
        
        assert!(result.is_ok());
        
        // 应该回到关闭状态
        let state = cb.state("test-service").await;
        assert_eq!(state, CircuitBreakerState::Closed);
    }

    #[tokio::test]
    async fn test_circuit_breaker_metrics() {
        let config = CircuitBreakerConfig::default();
        let cb = DefaultCircuitBreaker::new(config);
        
        // 成功调用
        let _ = cb.call(
            "test-service",
            Box::new(|| {
                Box::pin(async {
                    Ok(Response::new(200))
                })
            })
        ).await;
        
        // 失败调用
        let _ = cb.call(
            "test-service",
            Box::new(|| {
                Box::pin(async {
                    Err(ServiceError::NetworkError("Connection failed".to_string()))
                })
            })
        ).await;
        
        let metrics = cb.metrics("test-service").await;
        assert_eq!(metrics.total_calls, 2);
        assert_eq!(metrics.success_calls, 1);
        assert_eq!(metrics.failed_calls, 1);
        assert!(metrics.last_failure_time.is_some());
    }

    #[tokio::test]
    async fn test_circuit_breaker_different_services() {
        let config = CircuitBreakerConfig::default();
        let cb = DefaultCircuitBreaker::new(config);
        
        // 服务A失败
        let _ = cb.call(
            "service-a",
            Box::new(|| {
                Box::pin(async {
                    Err(ServiceError::NetworkError("Connection failed".to_string()))
                })
            })
        ).await;
        
        // 服务B成功
        let result = cb.call(
            "service-b",
            Box::new(|| {
                Box::pin(async {
                    Ok(Response::new(200))
                })
            })
        ).await;
        
        assert!(result.is_ok());
        
        // 两个服务应该有独立的状态
        let state_a = cb.state("service-a").await;
        let state_b = cb.state("service-b").await;
        
        assert_eq!(state_a, CircuitBreakerState::Closed); // 还没达到阈值
        assert_eq!(state_b, CircuitBreakerState::Closed);
    }

    #[tokio::test]
    async fn test_circuit_breaker_config_validation() {
        let config = CircuitBreakerConfig {
            enabled: true,
            failure_threshold: 5,
            recovery_timeout: Duration::from_millis(30000),
            half_open_success_threshold: 3,
        };
        
        let cb = DefaultCircuitBreaker::new(config);
        
        // 验证初始状态
        let state = cb.state("new-service").await;
        assert_eq!(state, CircuitBreakerState::Closed);
        
        let metrics = cb.metrics("new-service").await;
        assert_eq!(metrics.total_calls, 0);
        assert_eq!(metrics.success_calls, 0);
        assert_eq!(metrics.failed_calls, 0);
    }

    #[tokio::test]
    async fn test_circuit_breaker_disabled() {
        let mut config = CircuitBreakerConfig::default();
        config.enabled = false;
        let cb = DefaultCircuitBreaker::new(config);
        
        // 即使有很多失败，熔断器也不应该打开
        for _ in 0..10 {
            let _ = cb.call(
                "test-service",
                Box::new(|| {
                    Box::pin(async {
                        Err(ServiceError::NetworkError("Connection failed".to_string()))
                    })
                })
            ).await;
        }
        
        let state = cb.state("test-service").await;
        assert_eq!(state, CircuitBreakerState::Closed);
    }

    #[test]
    fn test_circuit_breaker_state_transitions() {
        // 测试状态机转换逻辑
        assert_eq!(CircuitBreakerState::default(), CircuitBreakerState::Closed);
        
        // 验证状态枚举的序列化
        let closed = CircuitBreakerState::Closed;
        let open = CircuitBreakerState::Open;
        let half_open = CircuitBreakerState::HalfOpen;
        
        assert_ne!(closed, open);
        assert_ne!(open, half_open);
        assert_ne!(half_open, closed);
    }
}